
import { createServer } from "http";
import { Server } from "socket.io";

const httpServer = createServer();
const io = new Server(httpServer, {
  cors: {
    origin: "http://localhost:3000"
  }
});

io.on('connection', (socket) => {
  console.log(socket.id + "is connected !");

  socket.on('message',(message)=>{
    console.log(`${socket.id}: ${message}`);

    //broadcast message to all clients
    socket.broadcast.emit('message',JSON.stringify({
      user: socket.id,
      msg: message
    }));
  });
});

io.listen(4000)